package cn.com.headfree.point.dao.impl;

import cn.com.headfree.entity.TPersonYearPointDetail;
import cn.com.headfree.entity.TPointRuleInfo;
import cn.com.headfree.point.dao.PersonYearPointDetailDao;
import cn.com.headfree.point.dao.PointRuleDao;
import cn.com.headfree.vo.TPersonYearPointDetailVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.*;


@Repository("personYearPointDetailDao")
public class PersonYearPointDetailDaoImpl extends BaseDaoImpl<TPersonYearPointDetail> implements PersonYearPointDetailDao {


	@Override
	public void findPersonYearPointDetailPageList(Page<TPersonYearPointDetailVo> page, TPersonYearPointDetailVo personYearPointDetail) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		String sql = "select t.*,o.is_over,s.audit_status from t_person_year_point_detail t " ;
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" LEFT JOIN t_point_set_over o on t.year = o.year and t.org_id = o.org_id ");
		sqlBuilder.append(" LEFT JOIN t_salary_info s on s.id = t.salary_id and s.del_sign = 0 ");
		sqlBuilder.append(" where t.del_sign = 0  ");
		sqlBuilder.append(" AND t.org_id=? ");
		arrayList.add(personYearPointDetail.getOrgId());
		if (personYearPointDetail.getYear() != null) {
			sqlBuilder.append(" AND t.year= ? ");
			arrayList.add(personYearPointDetail.getYear());
		}
        if(!StringUtil.isNullOrEmpty(personYearPointDetail.getUserName())){
            sqlBuilder.append(" AND t.user_name LIKE concat('%',?,'%') ");
            arrayList.add(personYearPointDetail.getUserName());
        }
		if(!StringUtil.isNullOrEmpty(personYearPointDetail.getErpNo())){
			sqlBuilder.append(" AND t.erp_no =? ");
			arrayList.add(personYearPointDetail.getErpNo());
		}
		if(!StringUtil.isNullOrEmpty(personYearPointDetail.getDeptId())){
			sqlBuilder.append(" AND t.dept_id =? ");
			arrayList.add(personYearPointDetail.getDeptId());
		}
		if(!StringUtil.isNullOrEmpty(personYearPointDetail.getAuditStatus())){
			sqlBuilder.append(" AND s.audit_status =? ");
			arrayList.add(personYearPointDetail.getAuditStatus());
		}
		sqlBuilder.append(" order by t.create_time desc ");
		this.findPageBySql(TPersonYearPointDetailVo.class, sqlBuilder.toString(), page, arrayList.toArray());
	}

	@Override
	public List<TPersonYearPointDetail> findPersonYearPointDetailList(TPersonYearPointDetailVo personYearPointDetail) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
//		String sql = "select t.* from t_person_year_point_detail t where t.del_sign = 0 " ;
//		StringBuilder sqlBuilder = new StringBuilder(sql);
//		sqlBuilder.append(" AND t.org_id=? ");
		String sql = "select t.* from t_person_year_point_detail t " ;
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" LEFT JOIN t_salary_info s on s.id = t.salary_id and s.del_sign = 0 ");
		sqlBuilder.append(" where t.del_sign = 0  ");
		sqlBuilder.append(" AND t.org_id=? ");

		arrayList.add(personYearPointDetail.getOrgId());
		if (personYearPointDetail.getYear() != null) {
			sqlBuilder.append(" AND t.year= ? ");
			arrayList.add(personYearPointDetail.getYear());
		}
		if(!StringUtil.isNullOrEmpty(personYearPointDetail.getUserName())){
            sqlBuilder.append(" AND t.user_name LIKE concat('%',?,'%') ");
			arrayList.add(personYearPointDetail.getUserName());
		}
		if(!StringUtil.isNullOrEmpty(personYearPointDetail.getErpNo())){
			sqlBuilder.append(" AND t.erp_no =? ");
			arrayList.add(personYearPointDetail.getErpNo());
		}
        if(!StringUtil.isNullOrEmpty(personYearPointDetail.getDeptId())){
            sqlBuilder.append(" AND t.dept_id =? ");
            arrayList.add(personYearPointDetail.getDeptId());
        }
        if(!StringUtil.isNullOrEmpty(personYearPointDetail.getAuditStatus())){
			sqlBuilder.append(" AND s.audit_status =? ");
			arrayList.add(personYearPointDetail.getAuditStatus());
		}
		sqlBuilder.append(" order by create_time desc ");
		return this.findAllBySQL(TPersonYearPointDetail.class, sqlBuilder.toString(),null,false,arrayList.toArray());
	}

	@Override
	public void saveInfo(TPersonYearPointDetail personYearPointDetail) {
		this.save(personYearPointDetail);
	}

	@Override
	public List<TPersonYearPointDetail> getTPersonYearPointDetailInfo(String salaryId) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		String sql = "select t.* from t_person_year_point_detail t where t.del_sign = 0 " ;
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" AND t.salary_id = ? ");
		arrayList.add(salaryId);
		sqlBuilder.append(" order by create_time desc ");
		return this.findAllBySQL(TPersonYearPointDetail.class, sqlBuilder.toString(),null,false,arrayList.toArray());
	}

	@Override
	public void deletePersonYearPointDetailInfo(String salaryId) {
		Map<String,Object> params = new HashMap<>();
		String sql = " delete from t_person_year_point_detail where salary_id = #{salaryId,jdbcType=VARCHAR} ";
		params.put("salaryId",salaryId);
		this.executeSql(sql,params);
	}

	@Override
	public TPersonYearPointDetailVo getPersonalYearPointDetail(String year, String orgId,String erpNo) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		String sql = "select t.*,s.audit_status from t_person_year_point_detail t " ;
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" LEFT JOIN t_salary_info s on s.id = t.salary_id and s.del_sign = 0 ");
		sqlBuilder.append(" where t.del_sign = 0 ");
		sqlBuilder.append(" AND t.year=? ");
		sqlBuilder.append(" AND t.org_id=? ");
		sqlBuilder.append(" AND t.erp_no=? ");
		if (!StringUtil.isNullOrEmpty(year)) {
			arrayList.add(year);
		}
        if(!StringUtil.isNullOrEmpty(orgId)){
            arrayList.add(orgId);
        }
		if(!StringUtil.isNullOrEmpty(erpNo)){
			arrayList.add(erpNo);
		}
		sqlBuilder.append(" order by t.create_time desc ");
		return this.findUnique(TPersonYearPointDetailVo.class, sqlBuilder.toString(), arrayList.toArray());
	}
}
